<?php
/**
 * 会员管理
 * @author Ice <iceinto@mallog.com.cn>
 */
class MemberAction extends CommonAction
{
	public function _initialize() {
		parent::_initialize ();
		$this->assign('nav', 8);
        $this->assign('web_title', '用户中心');
	}

	/**
	 * 用户默认列表
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function index(){
		$this->display();
	}

	public function passwd(){
		$id = $this->getLoginUserInfo();
		if($id){
			$user = $this->getUser($id);
			if(isPost()){
				if($user['passwd'] != md5(getPost('oldPasswd'))){
					$this->errorLang('_OLD_PASSWD_ERROR');
				}
				//判断老密码是否正确
				$passwd = hh(getPost('passwd'));
				$repasswd = hh(getPost('repasswd'));
				if($passwd && $repasswd){
					if($passwd == $repasswd){
						$data = array();
						$data['passwd'] = md5($passwd);
						$data['id'] = $user['id'];
						if(M('User')->save($data)){
							$this->successLang('_UPDATE_SUCCESS');
						}else{
							$this->errorLang('_PASSWD_UPDATE_LOSE');
						}
					}else{
						$this->errorLang('_PASSWD_AND_REPASSWD_MUST_EQUAL');
					}
				}else{
					$this->errorLang('_PASSWD_AND_REPASSWD_NO_EMPTY');
				}
			}
		}else{
			$this->errorLang('_USER_ID_ERROR');
		}
		$this->display();
	}
	/**
	 * Get user
	 * @param int $id user id
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	private function getUser($id){
		$user =M('User')->find($id);
		if($user){
			$this->assign('user', $user);
			return $user;
		}else{
			$this->errorLang('_USER_ID_ERROR');
		}
	}

    public function autoPull()
    {
        $uid = $this->getLoginUserInfo();
        $isAuto = intval(getGet('auto'));
        if ($uid) {
            $data = array();
            $data['is_auto_pull'] = $isAuto;
            if (M('User')->where('id=' . $uid)->save($data)) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        }
    }
}
?>